﻿using System.Web.Mvc;
using System.Web.Security;
using EYCmsAdmin.Presentation.IPresentationServices;
using EYCmsAdmin.Presentation.Models.LoginModels;

namespace EYCmsAdmin.Presentation.Controllers
{
    public class AccountController : Controller
    {
        private readonly IAccountPresentationService accountPresentationService;

        public AccountController(IAccountPresentationService accountPresentationService)
        {
            this.accountPresentationService = accountPresentationService;
        }

        [HttpGet]
        public ActionResult LogOn()
        {
            return View("LogOn", new LoginModel());
        }

        [HttpPost]
        public ActionResult LogOn(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                SuccessLoginModel result = accountPresentationService.LogOn(model);
                if (result != null)
                {
                    return RedirectToAction("Index", "Home", new { sectionId = result.PermissionId });
                }

                return View("LogOn", model);
            }

            return View("LogOn", model);
        }

        [HttpGet]
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            Session.Clear();
            return RedirectToAction("LogOn");
        }
    }
}